package rubix.controller;

import rubix.model.*;
import java.util.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/poController")
public class PoController extends HttpServlet {
    
    private ArrayList<Po> poList;
    
    public ArrayList<Po> retrieveAll() {
    PoManager poManager = new PoManager();
    poList = poManager.retrieveAll();
    return poList;
    }
    /*    
    protected void processSetSupplierId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String supplierId = request.getParameter("supplierID");
        String todayDate = request.getParameter("todayDate");
        session.setAttribute("supplierID",supplierId);
        session.setAttribute("todayDate",todayDate);
        response.sendRedirect("createPurchaseOrder.jsp");
    }*/
    
    protected void processCreatePurchaseOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String i = request.getParameter("checkedItem");
        String username = (String) session.getAttribute("loggedIn");
        String supplierId = (String)session.getAttribute("supplierID");
        String itemId = request.getParameter("itemID" + i);
        String partId = request.getParameter("partID" + i);
        String partPrice = PartManager.retrievePart(partId).getPartPrice();
        String poIdTemp = "";
        ArrayList<Po> poList = PoManager.retrieveAll();
        for(Po po : poList) {
            poIdTemp = po.getPoId();
        }
        String temp1 = poIdTemp.substring(5);
        String temp2 = poIdTemp.substring(1,5);
        String temp3 = "";
        int x = 0;
        try {
            x = Integer.parseInt(temp1);
            x++;
        } catch(NumberFormatException e) { }
        if(x < 10) {
            temp3 = "0" + Integer.toString(x);
        } else {
            temp3 = Integer.toString(x);
        }
        String poId = "P" + temp2 + temp3;
        session.setAttribute("poID",poId);
        String purchaseDate = (String)session.getAttribute("todayDate");
        String itemQuantity = request.getParameter("itemQuantity");
        int onOrderQuantity = 0;
        try {    
            onOrderQuantity = Integer.parseInt(itemQuantity);
        } catch(NumberFormatException e) { }
        String currency = "USD";

        PoManager.addPo(new Po(poId,supplierId,purchaseDate,currency,itemId,partId,partPrice,onOrderQuantity));
        response.sendRedirect("purchaseOrderDetails.jsp?poID=" + poId);
    }   

    
    protected void processUpdatePurchaseOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String poId = (String)session.getAttribute("poID");
        Po po = PoManager.retrievePo(poId);
        String supplierId = po.getSupplierId();
        String purchaseDate = po.getPurchaseDate();
        String currency = po.getCurrency();
        String itemId = po.getItemId();
        String partId = po.getPartId();
        String partPrice = po.getPartPrice();
        int onOrderQuantity = po.getOnOrderQuantity();
        String newOnOrderQuantityString = request.getParameter("Quantity");
        int newOnOrderQuantity = 0;
        try {
            newOnOrderQuantity = Integer.parseInt(newOnOrderQuantityString);
        } catch(NumberFormatException e) { }
        
        PoManager.updateOnOrderQuantity(poId,newOnOrderQuantity);
        response.sendRedirect("purchaseOrderDetails.jsp?poID=" + poId);
    }     
    
    protected void processDeletePurchaseOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String poId = (String)session.getAttribute("poID");
        Po po = PoManager.retrievePo(poId);
        String supplierId = po.getSupplierId();
        String purchaseDate = po.getPurchaseDate();
        String currency = po.getCurrency();
        String itemId = po.getItemId();
        String partId = po.getPartId();
        String partPrice = po.getPartPrice();
        int onOrderQuantity = po.getOnOrderQuantity();
        
        PoManager.deletePo(poId);
        response.sendRedirect("purchaseOrderDetails.jsp?poID=" + poId);
    }     
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if(session.getAttribute("poID")!=null) {
            session.removeAttribute("poID");
            response.sendRedirect("index.jsp");
        }
    }         
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if (session.getAttribute("supplierID")!= null && request.getParameter("checkedItem")!= null) {
            processCreatePurchaseOrder(request, response);
        } else if (session.getAttribute("poID") != null) {
            if(request.getParameter("delete") != null) {
                processDeletePurchaseOrder(request, response);
            } else {
                processUpdatePurchaseOrder(request, response);
            }
        } else if (request.getParameter("poID") != null) {
            processUpdatePurchaseOrder(request, response);
        } else {
            //
        }
    }
}